<?php
/**
 * Created by PhpStorm.
 * User: liu
 * Date: 2018/9/12
 * Time: 0:02
 */

namespace app\blog\model;


use app\blog\validate\Comments;
use think\facade\Request;
use think\Model;

class Comment extends Model
{
    protected $name='post_comment';
    protected $insert=['agent','ip'];

    public $autoWriteTimestamp='int';
    protected $createTime='created';
    protected $updateTime='updated';


    public function setIpAttr(){
        return ip2long(Request::ip());
    }
    public function getIpAttr($value){
        return long2ip($value);
    }

    public function setAgentAttr(){
        return Request::header('user-agent');
    }

    public static function add($param){
        $validate=new Comments();
        if(!$validate->check($param)){
            return $validate->getError();
        }
        $model=new self();
        return $model->allowField(true)
            ->isUpdate(false)
            ->save($param);
    }
}